package com.vpclub.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.vpclub.bean.Yellow;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;

/**
 *  Profile:
 *      Spring为我们提供的可以根据当前环境，动态的激活和切换一系列组件的功能
 *
 *  开发环境，测试环境，生产环境：
 *  数据源：(/A)(/B)(/C)
 *
 *  @Profile:指定组件在哪个环境的情况下才能被注册到容器中，不指定，任何环境下都能注册这个组件
 *          1.加了环境标识的bean，只有这个环境被激活的时候才能注册到容器中。默认是default环境
 *          2.写在配置类上，只有是指定的环境的时候，整个配置类里面所有的配置才能开始生效
 *          3.没有标注环境标识的bean在任何环境下都是加载的
 *
 *
 * @author : leimingchao
 * @date Date : 2021年06月23日 21:14
 */
@Configuration
@PropertySource(value = "classpath:/jdbc.properties")
public class MainConfigOfProfile implements EmbeddedValueResolverAware {

    @Value(value = "${db.user}")
    private String user;

    private StringValueResolver valueResolver;

    private String driverClass;


    @Bean
    public Yellow yellow() {
        return new Yellow();
    }


    @Profile(value = "test")
    @Bean(name = "testDataSource")
    public DataSource dataSourceTest(@Value(value = "${db.password}") String password) throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setDriverClass(driverClass);
        return null;
    }
    @Profile(value = "dev")
    @Bean(name = "devDataSource")
    public DataSource dataSourceDev(@Value(value = "${db.password}") String password) throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis");
        dataSource.setDriverClass(driverClass);
        return null;
    }

    @Profile(value = "prod")
    @Bean(name = "prodDataSource")
    public DataSource dataSourceProd(@Value(value = "${db.password}") String password) throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/tx");
        dataSource.setDriverClass(driverClass);
        return null;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.valueResolver = resolver;
        this.driverClass = valueResolver.resolveStringValue("${db.driverClass}");
    }
}
